//
//  XHMainTabBarController.swift
//  XHWeibo
//
//  Created by craneteng on 16/4/6.
//  Copyright © 2016年 XHTeng. All rights reserved.
//

import UIKit

class XHMainTabBarController: UITabBarController {
    
    /// 点击了中间的编辑按钮事件
    @objc private func editButtonClick() {
        print("点击了编辑按钮")
    }
    
    /// 懒加载中间的按钮
    private lazy var centerButton : UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        // 会根据背景图片的大小调整尺寸
        //        button.sizeToFit()
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加子控制器
        addChildViewControllers()
    }
    
    // 在viewWillAppear 之后添加按钮
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 设置中间按钮
        addCenterButton()
    }
    
    private func addCenterButton() {
        tabBar.addSubview(centerButton)
        let count = childViewControllers.count;
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        // 两边缩小图片
        centerButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
        centerButton.addTarget(self, action: "editButtonClick", forControlEvents: UIControlEvents.TouchUpInside);
    }
    
    // MARK: - 设置所有控制器
    private func addChildViewControllers() {
        addChildViewController(XHHomeTableViewController(), vcTitle: "首页",imageName: "tabbar_home")
        addChildViewController(XHMessageTableViewController(), vcTitle: "消息",imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(XHDiscoverTableViewController(), vcTitle: "搜索",imageName: "tabbar_discover")
        addChildViewController(XHProfileTableViewController(), vcTitle: "我",imageName: "tabbar_profile")
    }
    
    // MARK: - 设置每个子控制器
    private func addChildViewController(viewController : UIViewController , vcTitle : String , imageName : String) {
        let vc = viewController
        vc.title = vcTitle
        let nav = UINavigationController(rootViewController: vc)
        
        nav.tabBarItem.image = UIImage(named: imageName)
        
        addChildViewController(nav)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
